package com.rnkrsoft.suyan.config.server.ajax.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringHelper implements ApplicationContextAware {
    private static ApplicationContext APPLICATION_CONTEXT;

    public static  <T> T getBean(Class<T> clazz){
        validate();
        return APPLICATION_CONTEXT.getBean(clazz);
    }

    private static void validate(){
        if (APPLICATION_CONTEXT == null){
            throw new RuntimeException("please config <context:scan basePackage=\"com.rnkrsoft.suyan.config.server.ajax.spring.SpringHelper\"/> or " +
                    "@Import(com.rnkrsoft.suyan.config.server.ajax.spring.SpringHelper)");
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.APPLICATION_CONTEXT = applicationContext;
    }
}
